package com.jwetherell.augmented_reality.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;

import com.jwetherell.augmented_reality.R;
import com.jwetherell.augmented_reality.ui.IconMarker;
import com.jwetherell.augmented_reality.ui.Marker;

import edu.fiu.cis.objects.BIMLine;
import edu.fiu.cis.objects.BIMMarker;

/**
 * This class should be used as a example local data source. It is an example of
 * how to add data programatically. You can add data either programatically,
 * SQLite or through any other source.
 * 
 * @author Justin Wetherell <phishman3579@gmail.com>
 */
public class LocalDataSource extends DataSource {
	private static final int INVALID = -1000;
	private static final int MULTIPLICATION_FACTOR = 10;
	private String name;
	private double latitude;
	private double longitude;
	private ArrayList<BIMLine> lineList;

    private List<Marker> cachedMarkers = new ArrayList<Marker>();
    private static Bitmap icon = null;
    private Resources resource;

    public LocalDataSource(Resources res) {
        if (res == null) throw new NullPointerException();
        this.resource = res;
        this.lineList = new ArrayList<BIMLine>();
        try {
			this.loadXML();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        createIcon(res);
    }

    protected void createIcon(Resources res) {
        if (res == null) throw new NullPointerException();

        icon = BitmapFactory.decodeResource(res, R.drawable.icon);
    }

    public List<Marker> getMarkers() {
//        Marker atl = new IconMarker("ATL ICON", 25.7476000, -80.3525500, 0, Color.DKGRAY, icon);
//        cachedMarkers.add(atl);

        //25.7476000, -80.3525500
    	
        BIMMarker home = new BIMMarker(this.resource,this.lineList, this.name,this.latitude, this.longitude, 0, Color.BLUE);
        cachedMarkers.add(home);

        /*
         * Marker lon = new IconMarker(
         * "I am a really really long string which should wrap a number of times on the screen."
         * , 39.95335, -74.9223445, 0, Color.MAGENTA, icon);
         * cachedMarkers.add(lon); Marker lon2 = new IconMarker(
         * "2: I am a really really long string which should wrap a number of times on the screen."
         * , 39.95334, -74.9223446, 0, Color.MAGENTA, icon);
         * cachedMarkers.add(lon2);
         */

        /*
         * float max = 10; for (float i=0; i<max; i++) { Marker marker = null;
         * float decimal = i/max; if (i%2==0) marker = new Marker("Test-"+i,
         * 39.99, -75.33+decimal, 0, Color.LTGRAY); marker = new
         * IconMarker("Test-"+i, 39.99+decimal, -75.33, 0, Color.LTGRAY, icon);
         * cachedMarkers.add(marker); }
         */

        return cachedMarkers;
    }
    public void loadXML() throws XmlPullParserException, IOException
	{
    	XmlResourceParser xpp = this.resource.getXml(R.xml.small);
    	xpp.next();
    	int eventType = xpp.getEventType();
    	String xmlName = xpp.getName();
    	while (eventType != XmlPullParser.END_DOCUMENT)
    	{
    		
    		if(eventType == XmlPullParser.START_TAG && xmlName.equals("Campus"))
    		{
    			while(eventType != XmlPullParser.END_TAG || !xmlName.equals("Campus"))
    			{
    				xmlName = xpp.getName();
    				if(eventType == XmlPullParser.START_TAG && xmlName.equals("Location"))
    				{
    					while(eventType != XmlPullParser.END_TAG || !xmlName.equals("Location"))
    					{
    						
    						if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("Name"))
    						{
    							xpp.next();
    							this.name  = xpp.getText();
    							Log.i("Rajin","Got name:"+name);
    						}
    						else if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("Latitude"))
    						{
    							xpp.next();
    							this.latitude = Double.parseDouble( xpp.getText());
    							
    						}
    						else if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("Longitude"))
    						{
    							xpp.next();
    							this.longitude = Double.parseDouble( xpp.getText());
    						}
    						eventType = xpp.next();
    						xmlName = xpp.getName();
    					}
    				}
    				else if(eventType == XmlPullParser.START_TAG && xmlName.equals("Building"))
    				{
    					while(eventType != XmlPullParser.END_TAG || !xmlName.equals("Building"))
    					{
    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("BuildingStorey"))
    						{
    							while(eventType != XmlPullParser.END_TAG || !xmlName.equals("BuildingStorey"))
    	    					{
    	    						
    	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("PlanarGeometry"))
    	    						{
    	    							while(eventType != XmlPullParser.END_TAG || !xmlName.equals("PlanarGeometry"))
    	    	    					{
    	    	    						
    	    	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("PolyLoop"))
    	    	    						{

	    	    	    						double prevX = INVALID;
	    	    	    						double prevY = INVALID;
	    	    	    						double prevZ = INVALID;
    	    	    							while(eventType != XmlPullParser.END_TAG || !xmlName.equals("PolyLoop"))
    	    	    	    					{
    	    	    								double x = INVALID;
    	    	    								double y = INVALID;
    	    	    								double z = INVALID;
    	    	    	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("CartesianPoint"))
    	    	    	    						{
    	    	    	    							xpp.next();// star tag
    	    	    	    							xpp.next();// Value tag
    	    	    	    							String val = xpp.getText();
    	    	    	    							xpp.next();//endtag;
    	    	    	    							z = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
    	    	    	    							xpp.next();// star tag
    	    	    	    							xpp.next();// Value tag
    	    	    	    							val = xpp.getText();
    	    	    	    							xpp.next();//endtag;
    	    	    	    							x = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
    	    	    	    							
    	    	    	    							xpp.next();// star tag
    	    	    	    							xpp.next();// Value tag
    	    	    	    							y = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
    	    	    	    							xpp.next();//endtag; 
    	    	    	    							if(prevX != INVALID && prevY != INVALID && prevZ != INVALID)
    	    	    	    							{
    	    	    	    								BIMLine line = new BIMLine(prevX, prevY,prevZ, x, y,z);
    	    	    	    								
    	    	    	    								BIMLine line2 = new BIMLine(prevX, prevY+200,prevZ, x, y+200,z);
    	    	    	    								BIMLine line3 = new BIMLine(prevX, prevY,prevZ, prevX, prevY+200,prevZ);
    	    	    	    								BIMLine line4 = new BIMLine(x, y,z, x, y+200,z);
    	    	    	    								
    	    	    	    								this.lineList.add(line);
    	    	    	    								this.lineList.add(line2);
    	    	    	    								this.lineList.add(line3);
    	    	    	    								this.lineList.add(line4);
    	    	    	    							}
    	    	    	    							prevX = x;
    	    	    	    							prevY = y;
    	    	    	    							prevZ = z;
    	    	    	    									
    	    	    	    						}
    	    	    	    						
    	    	    	    						eventType = xpp.next();
    	    	    	    						xmlName = xpp.getName();
    	    	    	    					}
    	    	    						}
    	    	    						
    	    	    						eventType = xpp.next();
    	    	    						xmlName = xpp.getName();
    	    	    					}
    	    						}
    	    						
    	    						eventType = xpp.next();
    	    						xmlName = xpp.getName();
    	    					}
    						}
    						
    						eventType = xpp.next();
    						xmlName = xpp.getName();
    					}
    				}
    				/*
    				else if(eventType == XmlPullParser.START_TAG && xmlName.equals("Surface"))
    				{
    					while(eventType != XmlPullParser.END_TAG || !xmlName.equals("Surface"))
    					{
    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("PlanarGeometry"))
    						{
    							while(eventType != XmlPullParser.END_TAG || !xmlName.equals("PlanarGeometry"))
    	    					{
    	    						
    	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("PolyLoop"))
    	    						{

	    	    						double prevX = INVALID;
	    	    						double prevY = INVALID;
	    	    						double prevZ = INVALID;
    	    							while(eventType != XmlPullParser.END_TAG || !xmlName.equals("PolyLoop"))
    	    	    					{
    	    								double x = INVALID;
    	    								double y = INVALID;
    	    								double z = INVALID;
    	    	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("CartesianPoint"))
    	    	    						{
    	    	    							xpp.next();// star tag
    	    	    							xpp.next();// Value tag
    	    	    							String val = xpp.getText();
    	    	    							xpp.next();//endtag;
    	    	    							z = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
    	    	    							xpp.next();// star tag
    	    	    							xpp.next();// Value tag
    	    	    							val = xpp.getText();
    	    	    							xpp.next();//endtag;
    	    	    							x = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
    	    	    							
    	    	    							xpp.next();// star tag
    	    	    							xpp.next();// Value tag
    	    	    							y = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
    	    	    							xpp.next();//endtag; 
    	    	    							if(prevX != INVALID && prevY != INVALID && prevZ != INVALID)
    	    	    							{
    	    	    								BIMLine line = new BIMLine(prevX, prevY,prevZ, x, y,z);
    	    	    								
    	    	    								BIMLine line2 = new BIMLine(prevX, prevY+200,prevZ, x, y+200,z);
    	    	    								BIMLine line3 = new BIMLine(prevX, prevY,prevZ, prevX, prevY+200,prevZ);
    	    	    								BIMLine line4 = new BIMLine(x, y,z, x, y+200,z);
    	    	    								
    	    	    								this.lineList.add(line);
    	    	    								this.lineList.add(line2);
    	    	    								this.lineList.add(line3);
    	    	    								this.lineList.add(line4);
    	    	    							}
    	    	    							prevX = x;
    	    	    							prevY = y;
    	    	    							prevZ = z;
    	    	    									
    	    	    						}
    	    	    						
    	    	    						eventType = xpp.next();
    	    	    						xmlName = xpp.getName();
    	    	    					}
    	    						}
    	    						
    	    						eventType = xpp.next();
    	    						xmlName = xpp.getName();
    	    					}
    						}
    						
    						eventType = xpp.next();
    						xmlName = xpp.getName();
    					
    					}
    				}
    				//*/
    				eventType = xpp.next();
    				xmlName = xpp.getName();
    			}
    		}
    		
    		eventType = xpp.next();
    		xmlName = xpp.getName();
    	
    	}
    	
    	
	}
}
